package cn.stylefeng.guns.modular.shuheng.service;

import cn.stylefeng.guns.core.common.constant.Constants;
import cn.stylefeng.guns.modular.shuheng.entity.Region;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.stylefeng.guns.modular.shuheng.mapper.RegionMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author zhengpp
 * @since 2021-03-04
 */
@Service
public class RegionService extends ServiceImpl<RegionMapper,Region> {
    public Page<Map<String,Object>> listRegion(String regionName,Integer regionType,Long parentId) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.selectRegion(page,regionName,regionType,parentId);
    }

    /**
     * 创建编码
     * @param region
     * @return
     */
    public String getRegionCode(Region region) {
        Long parentId = region.getParentId();
        QueryWrapper<Region> wrapper = new QueryWrapper<Region>();
        wrapper.eq("parent_id",parentId);
        List<Region> regionList = baseMapper.selectList(wrapper);
        Integer maxNum = 0;
        int startPos = 0;
        int endPos = 0;
        if(Constants.INT_TWO==region.getRegionLevel()){
            //区县级
            startPos = 2;
            endPos = 4;
        }
        if(Constants.INT_THERE==region.getRegionLevel()){
            //区县级
            startPos = 4;
            endPos = 6;
        }
        String regionCodeStart = "";
        String regionCodeEnd = "";
        if(regionList!=null && regionList.size()>0){
            for(int i=0;i<regionList.size();i++){
                int num = Integer.parseInt(regionList.get(i).getRegionCode().substring(startPos,endPos));
                if(num>maxNum){
                    maxNum = num;
                }
                if(i==0){
                    regionCodeStart = regionList.get(i).getRegionCode().substring(0,startPos);
                    regionCodeEnd = regionList.get(i).getRegionCode().substring(endPos);
                }
            }
        }
        maxNum ++ ;
        String regionCode = regionCodeStart + maxNum + regionCodeEnd;
        region.setRegionCode(regionCode);
        return regionCode;
    }

    /**
     * 根据省id列出所有市
     * @return
     */
    public List<Region> listCity(Long province){
        QueryWrapper<Region> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",province);
        wrapper.eq("region_level",Constants.LEVEL_CITY);
        return this.baseMapper.selectList(wrapper);
    }

    public Region getByRegionName(String cityName) {
        QueryWrapper<Region> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",16L);
        wrapper.eq("region_name",cityName);
        return this.baseMapper.selectOne(wrapper);
    }

    /**
     * 根据省id列出所有区县
     * @return
     */
    public List<Region> listCounty(Long city) {
        QueryWrapper<Region> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",city);
        wrapper.eq("region_level",Constants.LEVEL_COUNTY);
        return this.baseMapper.selectList(wrapper);
    }
}
